package com.example.myblogserver.controller.article;

import com.example.myblogserver.common.result.Result;
import com.example.myblogserver.common.utils.RedisUtil;
import com.example.myblogserver.dto.AddArticleDTO;
import com.example.myblogserver.dto.UpdateArticleDTO;
import com.example.myblogserver.service.ArticleService;
import com.example.myblogserver.vo.CategoryVO;
import com.example.myblogserver.vo.VisitorVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
@Api(tags = "文章相关接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 新增文章
     * @param addArticleDTO
     * @return
     */
    @PostMapping("/articles")
    @ApiOperation(value = "新增文章")
    public Result<?> addArticle(@RequestBody AddArticleDTO addArticleDTO) {
        log.info("新增文章");
        // 设置用户id
        articleService.addArticle(addArticleDTO);
        return Result.success();
    }

    /**
     * 获取所有文章
     * @return
     */
    @GetMapping("/articles")
    @ApiOperation(value = "获取所有文章")
    public Result<Object> getAllArticles() {
        log.info("获取所有文章");
        return Result.success(articleService.getAllArticles());
    }

    /**
     * 获取分类文章列表
     * @param categoryId
     * @return
     */
    @GetMapping("/articles/{categoryId}")
    @ApiOperation(value = "获取分类文章列表")
    public Result<Object> getArticle(@PathVariable Long categoryId) {
        log.info("获取分类文章列表");
        return Result.success(articleService.getArticlesByCategoryId(categoryId));
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/article/{id}")
    @ApiOperation(value = "获取文章详情")
    public Result<Object> getArticleById(@PathVariable Long id) {
        log.info("获取文章详情");
        return Result.success(articleService.getArticleById(id));
    }
    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/article/{id}")
    @ApiOperation(value = "删除文章")
    public Result<?> deleteArticleById(@PathVariable Long id) {
        log.info("删除文章");
        articleService.deleteArticleById(id);
        return Result.success();
    }

    /**
     * 获取文章分类
     * @return
     */
    @GetMapping("/article/category")
    @ApiOperation(value = "获取文章分类")
    public Result<List<CategoryVO>> getArticleCategory() {
        log.info("获取文章分类");

        List<CategoryVO> categoryVOList = articleService.getArticleCategory();
        return Result.success(categoryVOList);
    }

        /**
     * 修改文章
     * @param id 文章ID
     * @param updateArticleDTO 文章数据
     * @return Result<?>
     */
    @PutMapping("/articles/{id}")
    @ApiOperation(value = "修改文章")
    public Result<?> updateArticle(
            @PathVariable Long id,
            @RequestBody UpdateArticleDTO updateArticleDTO) {
        log.info("修改文章，ID: {}", id);
        // 设置文章ID
        updateArticleDTO.setId(id);
        // 执行更新操作
        articleService.updateArticle(updateArticleDTO);
        return Result.success();
    }

    /**
     * 新增接口：获取文章访客记录
     * @param id 文章ID
     * @return 包含访客IP列表和总访问次数
     */
    @GetMapping("/article/visitors/{id}")
    @ApiOperation(value = "获取文章访客记录")
    public Result<VisitorVO> getArticleVisitors(@PathVariable Long id) {
        String today = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String ipKey = "article:visitor:ip:" + id + ":" + today;
        String countKey = "article:visit:count:" + id;

        return Result.success(VisitorVO.builder()
                .articleId(id)
                .visitorIps(redisUtil.getVisitorIps(ipKey))
                .totalVisits(redisUtil.getVisitCount(countKey))
                .build());
    }
}
